<template>
  <div>
    <el-form>
      <el-form-item label="编号" v-show="false">
        <el-input v-model="id"></el-input>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="name"></el-input>
      </el-form-item>
      <el-form-item label="跳转条件">
        <el-tooltip class="item" effect="light" placement="right">
          <template slot="content">
            审批通过或拒绝：<br/>
            approval==true（false）<br/>
          </template>
          <i class="el-icon-warning"></i>
        </el-tooltip>
        <el-input v-model="conditionExpress" ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import bpmnHelper from '../js/helper/BpmnHelper';
  import {is} from 'bpmn-js/lib/util/ModelUtil';
  import AdjacencyMatrixGraph from '../js/util/AdjacencyMatrixGraph';
  import {OperatorAllOptions,LogicOptions,wfResult,OperatorLessOptions} from '../../static/js/static';
  import {isBlank} from '../js/util/CommonUtils';
  import AdapterComponent from '../assistComponents/AdapterComponent.vue';
  import {IntAttri} from '../../static/js/static';

  const eventDefinitionHelper = require('../js/helper/EventDefinitionHelper'),
    isAny = require('bpmn-js/lib/features/modeling/util/ModelingUtil').isAny,
    elementHelper = require('../js/helper/ElementHelper'),
    forEach = require('lodash/forEach'),
    filter = require('lodash/filter'),
    find = require('lodash/find'),
    findIndex = require('lodash/findIndex'),
    includes = require('lodash/includes'),
    replace = require('lodash/replace'),
    map = require('lodash/map'),
    uuid = require('uuid/v1');
  const DEFAULT_VALUE = {
    condition:'未配置任何条件',
    logicLabel:'无'
  }
  export default {
    props:['element'],
    inject: ['bpmnModeler'],
    components:{AdapterComponent},
    data(){
      return {
        tempVarible:'',
        modeling: null,
        id : this.element.id || '',
        name: '',
        condition:DEFAULT_VALUE.condition,
        dialogTableVisible:false,
        //条件列表
        conditionList:[],
        logicOptions: LogicOptions,
        conditionalOpts:OperatorAllOptions,
        formShow:false,
        conditionExpress:undefined

      }
    },

    watch: {
      element: {
        deep: true,
        immediate: true,
        handler(element, oldVal) {
          this.name = element.businessObject.name;
          //显示条件配置选项
          //获取source节点
          let sourceBo = bpmnHelper.getBo(element.source);
          if (!sourceBo) {
            return;
          }
          //加载条件
          this.loadCondition();
        }
      },
      name: {
        handler(newVal, oldVal) {
          this.modeling.updateProperties(this.element,{'name': newVal});
        }
      },
      conditionExpress: {
        handler(newVal, oldVal) {
          this.saveConditions(newVal.trim())
        }
      },
    },

    mounted(){
      const bpmnModeler = this.bpmnModeler();
      this.modeling = bpmnModeler.get("modeling");
    },

    methods:{
      inputVarible(e) {
        console.log(e,"_________");
        let v = e.target.value;
        this.conditionList.push({TEXT:v,VALUE:v});
        this.form.variable = v;
      },

      //保存条件
      saveConditions(conditionExpress){
        let conditionOrConditionExpression = undefined;
        let bo = bpmnHelper.getBo(this.element);
        const bpmnModeler = this.bpmnModeler();
        const commandStack = bpmnModeler.get('commandStack');
        if(conditionExpress && conditionExpress.length > 0){
          let conditionalEventDefinition = eventDefinitionHelper.getConditionalEventDefinition(this.element);
          const bpmnFactory = bpmnModeler.get('bpmnFactory');

          //生成表达式
          let conditionExpress1 = '${' + conditionExpress + '}';
          //设置表达式的值
          conditionOrConditionExpression = elementHelper.createElement(
            'bpmn:FormalExpression',
            {body:conditionExpress1},
            conditionalEventDefinition || bo,
            bpmnFactory
          );
          let source = this.element.source;
          // 如果是默认条线，移除source的属性
          // if (source && source.businessObject.defaultflow) {
          //     bpmnHelper.updatePropertiesByCmd(source,commandStack,{ 'defaultflow': false });
          // }
        }
        bpmnHelper.updatePropertiesByCmd(this.element,commandStack,{conditionExpression: conditionOrConditionExpression});
      },

      removeRow(){
        if(this.table.selectedRow !== undefined){
          //删除选定的数组
          let index = findIndex(this.table.conditions,{id:this.table.selectedRow.id});
          this.table.conditions.splice(index,1);
          this.table.selectedRow = undefined;
        }
      },

      loadCondition(bo){
        bo = bo || bpmnHelper.getBo(this.element);
        let conditionalEventDefinition = eventDefinitionHelper.getConditionalEventDefinition(this.element);
        let conditionExpression = conditionalEventDefinition? conditionalEventDefinition.condition:bo.conditionExpression;
        if(conditionExpression && conditionExpression.get('body') && conditionExpression.get('body').length>3){
          //获取配置的条件表达
          let conditionExpress1 = conditionExpression.get('body');
          console.log('conditionExpress:'+conditionExpress1)
          this.conditionExpress = conditionExpress1.substr(2,conditionExpress1.length-3)
        }
      },
    },

  }
  //只有网关和活动才需要配置条件
  const CONDITIONAL_SOURCES = [
    'bpmn:Activity',
    'bpmn:ExclusiveGateway',
    'bpmn:InclusiveGateway',
    'bpmn:ComplexGateway'
  ];


</script>
<style>

</style>
